package com.ruoyi.quartz.task;

import com.ruoyi.system.domain.ParkingLot;
import com.ruoyi.system.service.IParkingLotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Component("parkTask")
public class ParkTask {


    @Autowired
    private IParkingLotService parkingLotService;


    public void parkTask() throws Exception {
//        ParkingLot parkingLot = parkingLotService.selectParkingLotById(1L);
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String s = format.format(date);
        Date parse = format.parse(s);
        ParkingLot parkingLot = parkingLotService.selectParkingLotByAddTime(parse);
        if (parkingLot == null){
            ParkingLot parkingLot1 = new ParkingLot();
            parkingLot1.setAddTime(date);
            parkingLot1.setTotalCount(360L);
            parkingLot1.setEmptyCount(0L);
            parkingLotService.insertParkingLot(parkingLot1);
        }else{
            Long totalCount = parkingLot.getTotalCount();
            Random random = new Random();
            // 模拟随机产生车
            int i = random.nextInt(totalCount.intValue());// 0-totalCount范围内的随机数
            parkingLot.setEmptyCount((long) i);
            parkingLot.setUpdateTime(date);
            parkingLotService.updateParkingLot(parkingLot);
        }

    }


}
